﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.SqlCommand;

namespace Infrastucture
{
   public class JurnalPembelianRepository :  BaseNHibernateRepository<JurnalPembelian>
   {

       public JurnalPembelianRepository(ISessionStorage sessionStorage)
           : base(sessionStorage)
       {
           
       }

       public string GetMaxNumber(string kodeAwal)
       {
           string res = string.Empty;
           var query = Session.CreateSQLQuery("Select Max(NoVoucher) as Number from JurnalPembelian  where NoVoucher like'" + kodeAwal + "%'").AddScalar("Number", NHibernateUtil.String);
           var objRes = query.List()[0];
           if (objRes != null) res = objRes.ToString();
           return res;
       }


       public IList<Jurnal> GetListByPeriodeAkun(DateTime startDate,DateTime endDate,string accountNumber)
       {
           IList<Jurnal> result;

              using (ITransaction tx = Session.BeginTransaction())
              {
                  ICriteria criteria = Session.CreateCriteria<Jurnal>()
                      .CreateAlias("JurnalPembelianDetails", "jd", JoinType.InnerJoin)
                      .Add(Restrictions.Between("Tanggal", startDate, endDate))
                      .Add(Restrictions.Eq("jd.AccountNumber", accountNumber));

                  result = criteria.List<Jurnal>();
                  tx.Commit();
              }

           return result;
       } 

    }
}
